/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action.base;

import com.daikit.commons.shared.bean.DkBaseUser;
import com.daikit.daikit4gxt.client.DkBaseMainController;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkChainActionCreator;
import com.daikit.daikit4gxt.client.log.DkLogger;


/**
 * The after connect action. It returns {@link DkBaseMainController#getSpecificAfterConnectAction(DkBaseUser)} with a
 * chain action creator reloading current screen and doing other stuff.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkBaseAfterConnectAction
{

	protected static final DkLogger log = DkLogger.getLog(DkBaseAfterConnectAction.class);

	/**
	 * Wrapper to {@link #get(DkBaseUser, DkBaseAction)} with a null actionToChain
	 * 
	 * @param previousLoggedUser
	 *           the previously logged user or null if no user was previously connected
	 * @return the base after connect action.
	 */
	public static final DkBaseAction<?> get(final DkBaseUser previousLoggedUser)
	{
		return get(previousLoggedUser, null);
	}

	/**
	 * If <code>current screen == null</code> it shows the current screen and reloads it.<br>
	 * Else If
	 * <code>BaseMain.loadDefaultScreenAfterEachLogin()<code> it shows the default screen and reloads it only if current
	 * logged user has changed.<br>
	 * Else it reloads the current screen only if current logged user has changed.<br>
	 * <br>
	 * If <code>actionToChain is not null</code> it is added at the end of the chain (only if current screen is not
	 * default screen and the default screen has to be shown after each connection). <br>
	 * 
	 * @param previousLoggedUser
	 *           the previously logged user or null if no user was previously connected
	 * @param actionToChain
	 *           the action to chain at the end of the after connect action.
	 * @return the base after connect action.
	 */
	public static final DkBaseAction<?> get(final DkBaseUser previousLoggedUser, final DkBaseAction<?> actionToChain)
	{
		final DkBaseAction<?> afterConnectAction = DkBaseMain.controller().getSpecificAfterConnectAction(previousLoggedUser);

		afterConnectAction.setLastChainActionCreator(new DkChainActionCreator()
		{
			@Override
			public DkBaseAction<?> getCreatedActions()
			{
				DkBaseAction<?> ret = null;
				if (DkBaseMain.controller().getCurrentScreen() == null)
				{
					ret = DkBaseShowScreenAction.get(DkBaseMain.controller().getDefaultScreen());
				}
				else
				{
					if (DkBaseMain.config().loadDefaultScreenAfterEachLogin()
							&& !DkBaseMain.controller().isCurrentScreen(DkBaseMain.controller().getDefaultScreen()))
					{
						log.debug("BaseMain.loadDefaultScreenAfterEachLogin() && !BaseMain.controller().isCurrentScreen(BaseMain.controller().getDefaultScreen())");
						log.debug("BaseMain.controller().getDefaultScreen()=" + DkBaseMain.controller().getDefaultScreen());
						log.debug("BaseMain.controller().getCurrentScreen()=" + DkBaseMain.controller().getCurrentScreen().getClass());
						ret = DkBaseShowScreenAction.get(DkBaseMain.controller().getDefaultScreen());
						ret.setLastChainActionCreator(new DkChainActionCreator()
						{
							@Override
							public DkBaseAction<?> getCreatedActions()
							{
								return !DkBaseMain.model().getUserLogged().equals(previousLoggedUser) ? DkBaseReloadCurrentScreenAction
										.get(true) : DkBaseInvalidateUiAction.get(true);
							}
						});
					}
					else if (!DkBaseMain.model().getUserLogged().equals(previousLoggedUser))
					{
						ret = DkBaseReloadCurrentScreenAction.get(true);
					}
					else
					{
						ret = DkBaseInvalidateUiAction.get(false);
						ret.setLastChainAction(actionToChain);
					}
				}
				return ret;
			}
		});
		return afterConnectAction;
	}
}
